<?php
class DB {

	var $MYSQL_ERR_SELECT_DB = 101;

	var $MYSQL_ERR_CONNECT = 102;

	var $MYSQL_SUCCESS = 100;

	var $connection = null;

	var $currentSchema = null;


	function DB() {
		if ($this->MYSQL_SUCCESS !=
			$this->Connect(DB_SCHEMA,DB_USERNAME,DB_PASSWORD,DB_HOST,DB_PORT) ) {

		// echo "<script>window.location =\"http://www.edianping.com\";</script>";
		die();
		}
		
		$this->Query("set names utf8");
		

	}

	function Connect( $dbschema = '', $dbuser = '', $dbpass = '', $dbhost = 'localhost', $dbport = '3306' ) {
		
		
		if ( ($this->connection =
			mysql_connect($dbhost . ':' . $dbport, $dbuser, $dbpass)) != false ) {
				
				if ( $this->setCurrentSchema($dbschema) == $this->MYSQL_SUCCESS ) 
				{
					return $this->MYSQL_SUCCESS;
				}
				else 
				{
					return $this->MYSQL_ERR_SELECT_DB;
				}
				
		}
		else {
			return $this->MYSQL_ERR_CONNECT;
		}
		
		
	}

	function SetCurrentSchema( $schema ) {
		$this->schema = $schema;
		if(mysql_select_db($this->schema, $this->connection)) {
			return $this->MYSQL_SUCCESS;
		}
		else {
			mysql_select_db(DB_SCHEMA, $this->connection);
			$sql = "select db_host, db_port from channel where dir_name='" . $this->schema . "'";
			$row = $this->GetRow($sql);
			if($dao->MYSQL_SUCCESS != $this->Connect($this->schema, DB_USERNAME, DB_PASSWORD,$row['db_host'],$row['db_port'])) {
				return $dao->MYSQL_SUCCESS;
			}
			else {
				return $this->MYSQL_ERR_SELECT_DB;
			}
		}
	}

	function GetCurrentSchema() {
		return $this->currentSchema;
	}

	/**
	 *
	 * @param string $sql
	 * @return string when query succeed, false when query failed
	 */
	function GetValue($sql) {
		$row = $this->GetRow($sql);
		if( $row != false )
			return $row[0];
		else
			return false;
	}

	/**
	 *
	 * @param string $sql
	 * @return array when query succeed, false when query failed
	 */
	function GetRow($sql) {
		$rs = $this->Query($sql);
		if ( $rs == false ) {
			return false;
		}
		$row = $this->fa($rs);
		@mysql_free_result($rs);
		return $row;
	}

	/**
	 *
	 * @param string $sql
	 * @return array when query succeed, false when query failed
	 */
	function GetCol($sql) {
		$rs = $this->Query($sql);
		if ( $rs == false ) {
			return false;
		}
		$data = array();
		while( ($row = $this ->fa($rs)) != false ) {
			$data[] = $row[0];
		}
		@mysql_free_result($rs);
		return $data;
	}

	/**
	 *
	 * @param string $sql
	 * @return array when query succeed, false when query failed
	 */
	function GetPlan($sql) {
		$rs = $this->Query($sql);
		if ( $rs == false ) {
			return false;
		}
		$data = array();
		while( ($row = $this ->fa($rs)) != false ) {
			$data[] = $row;
		}
		@mysql_free_result($rs);

		//to unique return model
		if( count($data) == 0 ){
			return false;
		}
		else{
			return $data;
		}
	}

	function Update($sql) {
		return @mysql_query($sql, $this->connection);
	}

	function Insert($sql) {
		return mysql_query($sql, $this->connection);
	}

	function LastID() {
		return mysql_insert_id($this ->connection);
	}

	function CountResultRows($rs) {
		return mysql_num_rows($rs);
	}

	function CountAffectedRows() {
		return mysql_affected_rows($this->connection);
	}

	function Disconnect() {
		return mysql_close($this->connection);
	}

	function Query($sql) {
		return @mysql_query($sql, $this->connection);
	}

	function fa($rs) {
		if(  !isset($this->assoc) ) {
			return @mysql_fetch_array($rs);
		}
		else {
			return @mysql_fetch_assoc($rs);
		}
	}

	function FormatValue( $theValue, $theType=null ,$slashes='gpc' ) {

		if ($slashes == 'gpc') {
			$theValue = get_magic_quotes_gpc() ? $theValue : addslashes($theValue);
		} elseif ($slashes == 'rt') {
			$theValue = get_magic_quotes_runtime() ? $theValue : addslashes($theValue);
		}



		if (empty($theType))
			$theType = gettype($theValue);

		switch ( $theType ) {
			case "integer":
				$theValue = ($theValue === '') ? "NULL"
					: intval($theValue) ;
				break;
			case "double":
				$theValue = ($theValue != '') ? "'".doubleval($theValue)."'"
					: "NULL";
				break;
			case "string":
				if ($theValue != "NOW()") {
					$theValue = "'" . $theValue . "'";
				}
				break;
			default :
				$theValue = "NULL";
				break;
		}
		Return $theValue;
	}

	function FormatField( $theField ) {
		return '`'.$theField.'`';
	}


    /**********************************************************
     *
     *	Query method supporting the iterator class below
     *	Seal Created On  2009-10-5
     *
     **********************************************************/

	function Execute( $sql ) {
		$result = @mysql_query($sql, $this->connection);
		$result_class = new QueryResult();
		$result_class->resource = $result;
		if( $this->CountAffectedRows() == 0 )
			$result_class->EOF = true;
		else {
			$zp_result_array = @mysql_fetch_array($result);

			if( !is_array($zp_result_array) ) {
				$result_class->EOF = true;
				return $result_class;
			}

			while (list($key, $value) = each($zp_result_array))
				if (!ereg('^[0-9]', $key))
					$result_class->fields[$key] = $value;
		}

		return $result_class;

	}
}

?>
